package com.example.arithmeticleetcode.leetcode;

import java.math.BigDecimal;
import java.util.*;

/**
 * @program: arithmetic-leetcode
 * @description: 1418. 点菜展示表
 * @author: FangZhen
 * @create: 2020-04-21 09:23
 **/
public class Demo05 {

    public static void main(String[] args) {
        String[][] arr = {{"James", "12", "Fried Chicken"}, {"Ratesh", "12", "Fried Chicken"},
                {"Amadeus", "12", "Fried Chicken"}, {"Adam", "1", "Canadian Waffles"},
                {"Brianna", "1", "Canadian Waffles"}};
        List<List<String>> list = new ArrayList<>();
        for (String[] arrStr : arr) {
            list.add(Arrays.asList(arrStr));
        }
        System.out.println(list);
        System.out.println("------------------------------------------------------");
        displayTable(list);

        System.out.println("===================================================");
        String str = "0";
        System.out.println(BigDecimal.ZERO.compareTo(new BigDecimal(str)));
        System.out.println(BigDecimal.ZERO.compareTo(new BigDecimal(str)) > 0);
    }

    /**
     * 输入：orders = {{"James","12","Fried Chicken"},{"Ratesh","12","Fried Chicken"},
     * {"Amadeus","12","Fried Chicken"},{"Adam","1","Canadian Waffles"},
     * {"Brianna","1","Canadian Waffles"}}
     * 输出：{{"Table","Canadian Waffles","Fried Chicken"},{"1","2","0"},{"12","0","3"}}
     * <p>
     * 来源：力扣（LeetCode）
     * 链接：https://leetcode-cn.com/problems/display-table-of-food-orders-in-a-restaurant
     * 著作权归领扣网络所有。商业转载请联系官方授权，非商业转载请注明出处。
     *
     * @param orders
     * @return
     */
    public static List<List<String>> displayTable(List<List<String>> orders) {
        LinkedList<List<String>> ans = new LinkedList<>();
        HashMap<String, HashMap> map = new HashMap<>(1024);
        HashSet<String> set = new HashSet<>();//存储第一行菜名的情况
        //建立map
        for (List<String> ll : orders) {
            String tableName = ll.get(1);
            String orderName = ll.get(2);
            set.add(orderName);
            HashMap<String, Integer> tmp;
            if (!map.containsKey(tableName)) {
                tmp = new HashMap<>(16);
            } else {
                tmp = map.get(tableName);
            }
            map.put(tableName, tmp);
            if (!tmp.containsKey(orderName)) {
                tmp.put(orderName, 1);
            } else {
                int c = tmp.get(orderName);
                tmp.put(orderName, c + 1);
            }
        }
        LinkedList<String> line1 = new LinkedList<>(set);
        //菜名按照字母排序
        Collections.sort(line1, new Comparator<String>() {
            @Override
            public int compare(String o1, String o2) {
                return o1.compareTo(o2);
            }
        });
        //建立表格
        for (Map.Entry<String, HashMap> entry : map.entrySet()) {
            LinkedList<String> tmp = new LinkedList<>();
            tmp.addFirst(entry.getKey());
            HashMap<String, Integer> fun = entry.getValue();
            for (String s : line1) {
                int cu = fun.getOrDefault(s, 0);
                tmp.add(String.valueOf(cu));
            }
            ans.add(tmp);
        }
        //把桌子编号排序
        Collections.sort(ans, new Comparator<List<String>>() {
            @Override
            public int compare(List<String> o1, List<String> o2) {
                return Integer.valueOf(o1.get(0)) - Integer.valueOf(o2.get(0));
            }
        });
        line1.addFirst("Table");
        ans.addFirst(line1);
        return ans;
    }
}
